package com.easybacklog.client.cucumberfeatures;

import com.easybacklog.client.EasyBacklogGlobalVariables;
import com.easybacklog.client.InitializeEasyBacklog;
import com.easybacklog.client.cucumberfeatures.edit.AceGWTEditor;
import com.easybacklog.client.cucumberfeatures.edit.BacklogFeatureFileView;
import com.easybacklog.client.cucumberfeatures.edit.BacklogFeatureForm;
import com.easybacklog.client.model.ProjectFeature;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;

public class BacklogFeaturesList extends FlexTable {

	public BacklogFeaturesList() {

		this.setWidth("650px");

		setBorderWidth(1);

		setCellPadding(5);

		gridColumns();

		gridRows();
	}

	private void gridColumns() {

		this.setHTML(0, 0, "<B>Feature Name</B>");
		this.getColumnFormatter().setWidth(0, "150px");

		this.setHTML(0, 1, "<B>Created on</B>");
		this.getColumnFormatter().setWidth(1, "110px");

		this.setHTML(0, 2, "<B>Priority Order</B>");
		this.getColumnFormatter().setWidth(2, "100px");

		this.setHTML(0, 3, "<B>Estimate</B>");
		this.getColumnFormatter().setWidth(3, "40px");

		this.setHTML(0, 4, "<B>Status</B>");
		this.getColumnFormatter().setWidth(4, "40px");

		this.setHTML(0, 5, "");
		this.getColumnFormatter().setWidth(5, "40px");
	}

	private void gridRows() {

		int row = 0;
		for (ProjectFeature entry : EasyBacklogGlobalVariables.projectFeatures
				.values()) {

			row++;

			this.setWidget(row, 0, getName(entry));

			if (entry.getDateModified() != null) {

				this.setText(row, 1, entry.getDateModified().toString());
			}

			String priority = "";
			if (entry.getPriority() != null) {
				priority = entry.getPriority().toString();
			}
			this.setText(row, 2, priority);

			String estimate = "";
			if (entry.getEstimate() != null) {
				estimate = entry.getEstimate().toString();
			}
			this.setText(row, 3, estimate);

			this.setText(row, 4, entry.getStatus());

			this.setWidget(row, 5, iconSource(entry));

		}
	}

	private HTML getName(final ProjectFeature backlogEntry) {

		HTML htmlName = new HTML("<a href=#>" + backlogEntry.getFeatureName()
				+ "</a>");

		htmlName.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				InitializeEasyBacklog.vpBacklog.clear();
				InitializeEasyBacklog.vpBacklog.add(new BacklogFeatureForm(
						backlogEntry));
			}
		});

		return htmlName;
	}

	private static HTML iconSource(final ProjectFeature backlogEntry) {

		HTML img = new HTML("<a href='#'><img src='" + GWT.getModuleName()
				+ "/easyBacklog/images/source.gif' ></a>");

		img.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				InitializeEasyBacklog.vpBacklog.clear();

				InitializeEasyBacklog.vpBacklog.add(new BacklogFeatureFileView(
						backlogEntry));

				new AceGWTEditor().showCodeEditor(backlogEntry);
			}
		});

		img.setTitle("View Source Code");

		return img;
	}
}
